<template>
  <div>
    <!--  #####################查询条件###################  -->
    <div align="center" style="margin-bottom: 20px; font-size: 30px"><b>{{this.record.nickName}}</b></div>
    <!--  #####################查询条件###################  -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="使用状态">
                <a-select v-model="queryParam.status" placeholder='请选择' allowClear>
                  <a-select-option value="1">未使用</a-select-option>
                  <a-select-option value="2">已使用</a-select-option>
                  <a-select-option value="3">已过期</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          <a-col :md="!advanced && 8 || 24" :sm="24">
            <a-button v-action:query type="primary" @click="$refs.table.refresh(true)">查询</a-button>
            <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!--  #####################操作###################  -->
    <div class="table-operator">
      <!--<a-button v-action:query type="primary" icon="plus" @click="handleAdd">新建</a-button>-->
      <a-button v-action:query icon="rollback" @click="handleGoBack">返回</a-button>
    </div>
    <!--  #####################操作###################  -->

    <!--  #####################表格###################  -->
    <s-table
      ref="table"
      :columns="columns"
      :data="loadData"
      bordered
    >
      <a-tag slot="status" slot-scope="text, record" :color="text | statusColorFilter">
        {{text | statusFilter}}
      </a-tag>
      <span slot="action" slot-scope="text, record">
        <template>
          <a v-action:edit v-if="record.status == 1" @click="handleUse(record)">一键核销</a>
        </template>
      </span>
    </s-table>
    <!--  #####################表格###################  -->

    <!--  #####################打开的页面###################  -->
    <!--<customer-pic-edit ref="createModal" @ok="handleOk"/>-->
    <!--  #####################打开的页面###################  -->
  </div>
</template>

<script>
  import moment from 'moment'
  import { STable } from '@/components'
  import { selectPage } from '@/api/shop/couponOrder'
  import { useMerCoupon } from '@/api/shop/coupon'

  const statusMap = {
    1: {
      status: '',
      text: '未使用'
    },
    2: {
      status: '#2db7f5',
      text: '已使用'
    },
    3: {
      status: '#f50',
      text: '已过期'
    }
  }
  const notityMap = {
    1: {
      status: 'red',
      text: '未领取'
    },
    2: {
      status: '#2db7f5',
      text: '已领取'
    },
  }

  export default {
    name: 'menuList',
    components: {
      STable
    },
    props: {
      record: {
        type: [Object, String],
        default: ''
      }
    },
    data () {
      return {
        treeData: [],
        // 高级搜索 展开/关闭
        advanced: false,
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '代金券名称',
            dataIndex: 'stockName'
          },
          {
            title: '编号',
            dataIndex: 'couponCode'
          },
          {
            title: '领取时间',
            dataIndex: 'createdTime',
            sorter: true
          },
          {
            title: '领取人',
            dataIndex: 'nickName'
          },
          {
            title: '核销状态',
            dataIndex: 'status',
            scopedSlots: { customRender: 'status' },
            sorter: true
          },
          {
            title: '核销时间',
            dataIndex: 'consumeTime',
            sorter: true
          },
          {
            title: '核销商户',
            dataIndex: 'merchantName'
          },
          {
            title: '核销订单号',
            dataIndex: 'transactionId'
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: '150px',
            scopedSlots: { customRender: 'action' }
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          parameter.miniOpenid = this.record.miniOpenid
          return selectPage(Object.assign(parameter, this.queryParam))
            .then(res => {
              return res.result
            })
        }
      }
    },
    created() {

    },
    filters: {
      statusFilter (status) {
        return statusMap[status].text
      },
      statusColorFilter (status) {
        return statusMap[status].status
      },
      sendFilter (status) {
        return notityMap[status].text
      },
      sendColorFilter (status) {
        return notityMap[status].status
      }
    },
    methods: {
      handleAdd () {
        this.$refs.createModal.add(this.record.id)
      },
      handleEdit (record) {
        this.$refs.createModal.edit(record)
      },
      handleUse (record) {
        const _this = this
        this.$confirm({
          title: '警告',
          content: `确定要手动核销 ${record.couponCode} 吗?`,
          okText: '确定',
          okType: 'danger',
          cancelText: '取消',
          onOk () {
            useMerCoupon({couponCode: record.couponCode}).then(res => {
              if (res.code === 200) {
                _this.$message.success(res.message)
                _this.$refs.table.refresh()
              }
              if (res.code === 500) {
                _this.$message.error(res.message)
              }
            })
          }
        })
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      handleGoBack () {
        this.$emit('onGoBack')
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      resetSearchForm () {
        this.queryParam = {
          date: moment(new Date())
        }
      }
    }
  }
</script>
